package com.tdkj.service.user.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.model.user.BeanExperienceAndIntegralCount;
import com.tdkj.model.user.BeanExperienceAndIntegralCountLog;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.user.IBeanExperienceAndIntegralCountLogService;
import com.tdkj.service.user.IBeanUserExperienceService;

@Component
public class BeanExperienceAndIntegralCountLogServiceImpl extends BaseService
		implements IBeanExperienceAndIntegralCountLogService {
	
	private IBeanUserExperienceService userExperienceService;
	    
    @Resource
	public void setUserExperienceService(IBeanUserExperienceService userExperienceService) {
		this.userExperienceService = userExperienceService;
	}

	@Override
	public void createCountLog(Long countId, Long userId, Long refId,
			Integer quantity) {
		BeanExperienceAndIntegralCount count = this.getBaseDao().findById(countId, BeanExperienceAndIntegralCount.class);
		BeanUser user = this.getBaseDao().findById(userId, BeanUser.class);
		
		int addExperience = userExperienceService.getAddExperience(count.getCode())*quantity;
		int addIntegral = userExperienceService.getAddIntegral(userId, count.getCode())*quantity;
		
		user.setExperience(user.getExperience() + addExperience);
		user.getFinancialAccount().setIntegral(user.getFinancialAccount().getIntegral() + addIntegral);
		
		
		BeanExperienceAndIntegralCountLog bean = new BeanExperienceAndIntegralCountLog();
		bean.setCount(count);
		bean.setUser(userExperienceService.findById(userId, BeanUser.class));
		bean.setRefId(refId);
		bean.setDateTime(new Timestamp(System.currentTimeMillis()));
		bean.setAddExperience(addExperience);
		bean.setAddIntegral(addIntegral);
		
		userExperienceService.save(bean);
	}

}
